program constraint_inherit;

class Packet1;
    randc  bit [3:0] addr;
    randc  bit [3:0] data;
    constraint addr_range { addr > 5; }
    constraint data_range { data < 8; }
endclass

class Packet2 extends Packet1;
    constraint addr_range { addr < 5; } // overriding constraint of parent class
    // inherits data_range from Packet1
endclass

initial begin
    Packet1 pkt1;
    Packet2 pkt2;

    pkt1 = new();
    pkt2 = new();

    $display("------------------------------------");
    repeat(10) begin
        pkt1.randomize();
        $display("\tpkt1: \t addr = %0d \t data = %0d", pkt1.addr, pkt1.data);
    end

    $display("------------------------------------"); 

    repeat(10) begin
        pkt2.randomize();
        $display("\tpkt2: \t addr = %0d \t data = %0d", pkt2.addr, pkt2.data);
    end
    $display("------------------------------------");
end

endprogram
